package cn.huangsm.advance.designmode.singlemode;

/**
 * 双重线程检查模式
 *
 * @author huang
 * @PACKAGE_NAME cn.huangsm.advance.designmode.singlemode
 * @PROJECT_NAME advance-code
 * @date 2019/1/24
 */
public class DoubleThreadMode {
    /**
     * 优点:延迟加载，线程安全
     * 缺点: 写法复杂
     */
    /**
     *  这里添加了volatile
     */
    private static volatile  DoubleThreadMode doubleThreadMode=null;

    private DoubleThreadMode(){}

    public static DoubleThreadMode getDoubleThreadMode(){
        //创建临时变量
        DoubleThreadMode inst=doubleThreadMode;
        if (inst==null){
            synchronized (DoubleThreadMode.class){
                inst=doubleThreadMode;
                if (inst==null){
                    inst=new DoubleThreadMode();
                    doubleThreadMode=inst;
                }
            }
        }
        //返回临时变量
        return inst;
    }
    protected  void method(){
        System.out.println("双重线程检查模式");
    }

    public static void main(String[] args) {
        DoubleThreadMode.getDoubleThreadMode().method();
    }
}
